﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;

public class TestDialog1 : BaseTestWindow
{

    private Rect window1;
    private Rect window2;
    private Rect window3;
    private Rect title;
    private Rect dragTop;
    private Rect content;
    private GUIStyle mS = new GUIStyle();
    private string mSelectStr = string.Empty;
    private bool mDrawLine = false;
    private DragDialogNode mSelectWindow;

    private Dictionary<int, DragDialogNode> mRectDic = new Dictionary<int, DragDialogNode>();
    private Dictionary<int, List<int>> mLineDic = new Dictionary<int, List<int>>();

    private GenericMenu mNodeMenu;
    private Event mEvent;
    private bool mIsDrag = false;


    public void Init()
    {
        window1 = new Rect(10, 50, 100, 100);
        window2 = new Rect(210, 210, 100, 100);
        window3 = new Rect(210, 210, 100, 100);
        title = new Rect(10, 10, 100, 100);
        dragTop = new Rect(0, 0, 210, 30);
        content = new Rect(0, 30, 210, 210);

        mS = new GUIStyle();

        mLineDic.Clear();
        mRectDic.Clear();
        mSelectWindow = null;


        List<DragDialogNode> wList = new List<DragDialogNode>();
        wList.Add(new DragDialogNode() { Id = 1, Rect = window1, Title = "1" });
        wList.Add(new DragDialogNode() { Id = 2, Rect = window2, Title = "2" });



        mRectDic.Add(wList[0].Id, wList[0]);
        mRectDic.Add(wList[1].Id, wList[1]);

        mNodeMenu = new GenericMenu();
        mNodeMenu.AddItem(new GUIContent("连线"), false, OnCreateNode, ENodeRightMenu.AddLine);
        mNodeMenu.AddItem(new GUIContent("删除"), false, OnCreateNode, ENodeRightMenu.Delete);

        this.maximized = true;
        Debug.Log("初始化");
    }

    private void OnGUI()
    {
        mEvent = Event.current;


        if (mEvent.type == EventType.Repaint)
        {
            GUIStyle style = "flow background";
            Rect scaledCanvasSize = new Rect(0, 0, Screen.width, Screen.height);
            style.Draw(scaledCanvasSize, false, false, false, false);
        }

        if (mEvent.type == EventType.MouseDrag)
        {
            mIsDrag = true;
        }
        else if (mEvent.type == EventType.DragExited)
        {
            mIsDrag = false;
        }
        else if (mEvent.type == EventType.MouseUp && mEvent.button == 1)  //右键
        {
            mNodeMenu.ShowAsContext();
            mEvent.Use();
            mIsDrag = false;
        }
        else if (mEvent.type == EventType.MouseDown && mDrawLine)
        {
            mSelectStr = "我放开了";
            if (mSelectWindow != null)
            {
                foreach (var item in mRectDic)
                {
                    DragDialogNode w = item.Value;
                    if (w.Id == mSelectWindow.Id)
                        continue;

                    if (w.Rect.Contains(mEvent.mousePosition))
                    {
                        if (mLineDic.ContainsKey(w.Id))
                        {
                            if (mLineDic[w.Id].Contains(mSelectWindow.Id))
                            {
                                Debug.Log("不能重复链接: " + w.Title + " : " + mSelectWindow.Title);
                                break;
                            }
                            else
                            {
                                mLineDic[w.Id].Add(mSelectWindow.Id);
                                Debug.Log("连接成功: " + w.Title + " : " + mSelectWindow.Title);
                            }
                        }
                        else
                        {
                            mLineDic.Add(w.Id, new List<int>
                                    {
                                    mSelectWindow.Id
                                    });
                            Debug.Log("连接成功: " + w.Title + " : " + mSelectWindow.Title);
                        }
                        break;
                    }
                }
            }
            mSelectWindow = null;
            mDrawLine = false;
        }


        BeginWindows();
        foreach (var item in mRectDic)
        {
            item.Value.Rect = GUI.Window(item.Value.Id, item.Value.Rect, DrawNodeWindow, item.Value.Title);
        }

        if (mDrawLine && mSelectWindow != null)
        {
            Handles.DrawLine(ToV3(mSelectWindow.Rect), mEvent.mousePosition);
        }

        DrawLine();
        GUILayout.Label("鼠标状态:" + mEvent.type.ToString() + "  " + mEvent.button);
        EndWindows();

    }

    private void DrawNodeWindow(int id)
    {
        GUILayout.Label(id.ToString());
        mSelectStr = GUILayout.TextField(mSelectStr);

        GUI.DragWindow(dragTop);

    }

    private void OnCreateNode(object key)
    {
        ENodeRightMenu v = (ENodeRightMenu)key;
        switch (v)
        {
            case ENodeRightMenu.AddLine:
                Debug.Log("点击连线");
                mDrawLine = true;
                mSelectWindow = GetMouseHoverNode();
                if (mSelectWindow == null)
                    Debug.LogError("鼠标没有在窗口上");
                break;
            case ENodeRightMenu.Delete:
                Debug.Log("删除");
                break;
            default:
                break;
        }
    }

    private DragDialogNode GetMouseHoverNode()
    {
        foreach (var item in mRectDic)
        {
            if (item.Value.Rect.Contains(mEvent.mousePosition))
                return item.Value;
        }

        return null;
    }

    private void DrawLine()
    {
        DragDialogNode a = null;
        DragDialogNode b = null;

        foreach (var item in mLineDic)
        {
            a = mRectDic[item.Key];
            for (int i = 0; i < item.Value.Count; i++)
            {
                b = mRectDic[item.Value[i]];

                Handles.DrawLine(ToV2(a.Rect), ToV2(b.Rect));
            }
        }
    }

    private void DrawNodeCurve(Rect start, Rect end)
    {
        Vector3 startPos = new Vector3(start.x + start.width, start.y + start.height / 2, 0);
        Vector3 endPos = new Vector3(end.x, end.y + end.height / 2, 0);
        Vector3 startTan = startPos + Vector3.right * 50;
        Vector3 endTan = endPos + Vector3.left * 50;
        Color shadowCol = new Color(0, 0, 0, 0.06f);


        for (int i = 0; i < 3; i++) // Draw a shadow
            Handles.DrawBezier(startPos, endPos, startTan, endTan, shadowCol, null, (i + 1) * 5);

        Handles.DrawBezier(startPos, endPos, startTan, endTan, Color.black, null, 1);
    }

    public Vector3 ToV3(Rect r)
    {
        Vector3 v3 = new Vector3(r.x + r.width / 2, r.y + r.height / 2, 0);
        return v3;
    }

    public Vector2 ToV2(Rect r)
    {
        Vector2 v2 = new Vector3(r.x + r.width / 2, r.y + r.height / 2);
        return v2;
    }

    [MenuItem("Plateface/Node editor2")]
    static void ShowEditor()
    {
        TestDialog1 editor = EditorWindow.GetWindow<TestDialog1>();
        editor.Init();
        editor.Show(true);
    }

}

public class DragDialogNode
{
    public string Title { get; set; }
    public int Id { get; set; }
    public Rect Rect { get; set; }
}

public enum ENodeRightMenu
{
    AddLine,
    Delete
}



